﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Wsitech.BLL;
using Wsitech.Utility;
using Wsitech.Model;
public partial class Backend_VideoAttachAdd : BackendPageBase
{
    protected readonly string videoDir = "/UploadFile/Video/";
    protected readonly int videoCoverWidth = 440;
    protected readonly int videoCoverHeight = 330;
    protected readonly int videoThumbWidth = 160;
    protected readonly int videoThumbHeight = 120;

    protected string tid = HttpContext.Current.Request["tid"] ?? "";
    protected string attachid = HttpContext.Current.Request["attachid"] ?? "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //设置默认路径
            //txtVideoPath.Text = videoDir;

            if (attachid != "")
            {
                initAttach(attachid);
            }
        }
    }

    //初始化信息
    private void initAttach(string attachid)
    {
        AttachInfo attach = AttachInfoBLL.Instance.GetDetail(attachid);
        if (attach != null)
        {
            addVideo.Visible = false;
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (attachid == "")
        {
            TextInfo info = TextInfoBLL.Instance.GetDetail(tid);
            if (info != null)
            {
                if (hfAttachPath.Value != "")
                {
                    string videoPath = Server.MapPath(hfAttachPath.Value);
                    if (!File.Exists(videoPath))
                    {
                        base.Alert("视频文件不存在！");
                        return;
                    }

                    string coverPath = AttachInfoBLL.Instance.GetVideoCoverPath(videoPath);
                    string thumbPath = AttachInfoBLL.Instance.GetThumbPath(coverPath);
                    if (hfCoverPath.Value != "")
                    {
                        try
                        {
                            ImageProcess.ImageToProperSize(Server.MapPath(hfCoverPath.Value), videoCoverWidth, videoCoverHeight, coverPath);
                            ImageProcess.ImageToProperSize(Server.MapPath(hfCoverPath.Value), videoThumbWidth, videoThumbHeight, thumbPath);
                        }
                        catch (Exception)
                        {
                            base.Alert("图片上传失败");
                            return;
                        }

                    }
                    else
                    {
                        VideoProcess.GetVideoCover(videoPath, videoCoverWidth, videoCoverHeight, 5, coverPath);
                    }


                    string videoLength = VideoProcess.GetVideoLength(videoPath);

                    AttachInfoBLL.Instance.AddVideoAttach(tid, info.Title, UtilityEnum.AttachType.Video, hfAttachPath.Value, videoLength);
                }
                else
                {
                    base.Alert("视频路径为空");
                }
            }
        }
        else
        {
            if (hfCoverPath.Value != "")
            {
                AttachInfo attach = AttachInfoBLL.Instance.GetDetail(attachid);
                if (attach != null)
                {
                    string coverPath = Server.MapPath(AttachInfoBLL.Instance.GetVideoCoverPath(attach.SourcePath));
                    string thumbPath = AttachInfoBLL.Instance.GetThumbPath(coverPath);
                    ImageProcess.ImageToProperSize(Server.MapPath(hfCoverPath.Value), videoCoverWidth, videoCoverHeight, coverPath);
                    ImageProcess.ImageToProperSize(Server.MapPath(hfCoverPath.Value), videoThumbWidth, videoThumbHeight, thumbPath);
                }
            }
        }
        base.RefreshParent();
    }
}